import 'package:aitao_flutter/common_ui/back_nav.dart';
import 'package:aitao_flutter/common_ui/confirm_btn.dart';
import 'package:aitao_flutter/common_ui/loading_widget.dart';
import 'package:aitao_flutter/model/user_data.dart';
import 'package:aitao_flutter/style/color_style.dart';
import 'package:aitao_flutter/style/edgeInsets_style.dart';
import 'package:aitao_flutter/utils/sp_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

class EditNamePage extends StatefulWidget {
  const EditNamePage({Key? key}) : super(key: key);

  @override
  State<EditNamePage> createState() => _EditNamePageState();
}

class _EditNamePageState extends State<EditNamePage> {
  UserData? userData;
  TextEditingController editingController = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: BackNavigation(
        title: '修改昵称',
      ),
      body: userData == null
          ? const LoadingWidget()
          : Column(
              children: [
                Container(
                    padding: EdgeInsetsStyle.all(10),
                    child: TextField(
                      controller: editingController,
                      decoration: InputDecoration(
                          suffixIcon: GestureDetector(
                            child: const Icon(
                              Icons.highlight_remove_rounded,
                              color: ColorStyle.ashGreyColor,
                            ),
                            onTap: () {
                              editingController.text = '';
                            },
                          ),
                          border: InputBorder.none,
                          hintText: '取个个性的名字'),
                    )),
                Container(
                  margin: EdgeInsetsStyle.edgeInsets(90, 50),
                  child: ConfirmButton(
                    borderRaduis: 8,
                    title: '确定',
                    titleColor: ColorStyle.whiteColor,
                    backGroundColor: ColorStyle.themeColor,
                    callBack: () {
                      if (editingController.text.isEmpty) {
                        return EasyLoading.showError('请输入昵称');
                      }
                      updateNickName();
                    },
                  ),
                )
              ],
            ),
    );
  }

  void updateNickName() async {
    Get.back(result: editingController.text);
  }

  @override
  void initState() {
    super.initState();
    getLocalUserData();
  }

  void getLocalUserData() {
    userData = SpUtil.getUserData();
    editingController.text = userData?.userinfo?.nickname ?? '';
    setState(() {});
  }
}
